package com.example.sinoyd.frameapplication.ProjectCodes.Fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.Chartdata
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.GVkeytime
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.GVkeyvalue
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.gson
import com.example.sinoyd.frameapplication.KotlinFrame.Frame.Uitl.Networkrequestmodel
import com.example.sinoyd.frameapplication.KotlinFrame.MpChart.CombinedChartDrawing
import com.example.sinoyd.frameapplication.KotlinFrame.Uitl.showdialog
import com.example.sinoyd.frameapplication.KotlinFrame.View.TimeSelectionBarView
import com.example.sinoyd.frameapplication.ProjectCodes.Dataclass.GetHourDataByDatetime
import com.example.sinoyd.frameapplication.R
import com.sinoyd.Code.Until.Networkrequestaddress
import com.sinoyd.Code.Until.SharedPreferencesFactory
import com.sinoyd.environmentsz.Kotlin.getDateofYesterday
import com.sinoyd.environmentsz.Kotlin.getToday
import kotlinx.android.synthetic.main.include_title_layout.*
import kotlinx.android.synthetic.main.real_time_monitoring_fragment.*
import okhttp3.Response
import org.jetbrains.anko.act
import org.jetbrains.anko.runOnUiThread
import org.jetbrains.anko.toast
import java.util.*
import kotlin.collections.ArrayList

/**
 * 实时监测界面
 * **/
@SuppressLint("ValidFragment")
class RealTimeMonitoringFragment(var name: String = "实时监测") : BaseFragment() {
    var date = Date()
    var startTime = date.getDateofYesterday()
    var endTime = date.getToday()
    var code = ""
    //选择站点后进行的操作
    override fun selectPortCallBack() {
        getHourDataByDatetime()
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val conview = inflater.inflate(R.layout.real_time_monitoring_fragment, null)
        return conview
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setview()
        setlisterens()
        getHourDataByDatetime()
    }

    override fun onResume() {
        super.onResume()

    }


    //设置界面
    private fun setview() {
        center_title.text = name
    }

    //设置各种监听
    private fun setlisterens() {
        //时间选择器监听
        timeselection.setdefaulttime(date.getDateofYesterday(), date.getToday())
                .setdefaulttextcolor("#000000")
                .setfaultimagecolor("#000000")
                .setTimeSelectionbarlistener(object : TimeSelectionBarView.TimeSelectionbarlistener {
                    override fun timeselectionbardoing(lefttime: String, righttime: String) {
                        startTime = lefttime
                        endTime = righttime
                        getHourDataByDatetime()
                    }
                }).show()

        //选择框
        selectionbarview.setdefaulttext("列表", "曲线图")
                .setdefaultSelectBt(selectionbarview.LEFTTRUE)
                .settextcolor("#FFFFFF")
                .setradiisleft(floatArrayOf(10f, 10f, 0f, 0f, 0f, 0f, 10f, 10f))
                .setradiisright(floatArrayOf(0f, 0f, 10f, 10f, 10f, 10f, 0f, 0f))
                .setradiobg("#550f82ff", "#0f82ff")
                .setOnCheckedChangeListener { radioGroup, i ->
                    when (i) {
                        R.id.rb_bt1 -> {
                            Twodimensional.visibility = View.VISIBLE
                            CombinedChart.visibility = View.GONE
                        }
                        R.id.rb_bt2 -> {
                            Twodimensional.visibility = View.GONE
                            CombinedChart.visibility = View.VISIBLE
                        }
                    }
                }
    }

    //获取实时监测数据

    fun getHourDataByDatetime() {
        showdialog(act, "loadshow")
        var ntrequest = Networkrequestmodel()
        ntrequest.setMethod(Networkrequestmodel.GETREQUEST)
                .settag("getHourDataByDatetime")
                .seturl(Networkrequestaddress.URL_GetHourDataByDatetime)
                .addparam("portId", portid)
                .addparam("startTime", startTime)
                .addparam("endTime", endTime)
                .addparam("sysType", SharedPreferencesFactory.getdata(act, "OS"))
                .addparam("code", code)
                .start(this)
    }


    var getHourDataByDatetime = GetHourDataByDatetime()
    var list曲线图: HashMap<String, ArrayList<Chartdata>> = HashMap()
    var list列表: ArrayList<GVkeyvalue> = ArrayList()
    override fun requestSuccess(response: Response, TAG: String) {
        super.requestSuccess(response, TAG)
        when (TAG) {
            "getHourDataByDatetime" -> {
                getHourDataByDatetime = gson.fromJson(responsestr, GetHourDataByDatetime::class.java)
                if (getHourDataByDatetime.list.size > 0) {
                    /***整理成我想要的列表数据***/
                    list列表 = ArrayList()
                    for (item in getHourDataByDatetime.list) {
                        for (it in item.datas) {
                            list列表.add(GVkeyvalue(GVkeytime(item.time, item.portName), GVkeytime(it.factor, ""), it.value))
                        }
                    }
                    /***整理成我想要的列表数据***/
                    //显示二维图表
                    runOnUiThread {
                        Twodimensional.initview("点位", false, false,80f).setdata(list列表).show()
                    }


                    /***整理成我想要的曲线图数据***/
                    //拿到所有因子
                    var listfactorname: ArrayList<String> = ArrayList()
                    for (item in getHourDataByDatetime.list) {
                        item.datas
                                .filterNot { it -> listfactorname.contains(it.factor) }
                                .forEach { it -> listfactorname.add(it.factor) }
                    }
                    //根据因子整理填充数据
                    for (item in listfactorname) {
                        var cd: Chartdata? = null
                        var Chartlist: ArrayList<Chartdata> = ArrayList()
                        var find: Boolean = false//是否找到值
                        for (it1 in getHourDataByDatetime.list) {
                            for (it2 in it1.datas) {
                                if (it2.factor == item) {
                                    cd = Chartdata(it1.time, it2.value)
                                    find = true
                                }
                            }
                            if (!find) {
                                cd = Chartdata(it1.time, "0")
                            }
                            Chartlist.add(cd!!)
                        }
                        list曲线图.put(item, Chartlist)
                    }
                    /***整理成我想要的数据***/
                    //画多个曲线图
                    runOnUiThread {
                        var ccd = CombinedChartDrawing(act, CombinedChart, list曲线图, CombinedChartDrawing.MORE_BROKEN_LINE, false, true, true, true)
                        ccd.initview().showchart()
                    }
                } else {
                    Looper.prepare()
                    toast("未获取到实时监测数据")
                    Looper.loop()
                }
            }
        }
    }
}